package com.zbu.friend.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbu.common.core.domain.R;
import com.zbu.common.core.domain.TableDataInfo;
import com.zbu.friend.domain.question.Question;
import com.zbu.friend.domain.question.dto.QuestionQueryDTO;
import com.zbu.friend.domain.question.es.QuestionES;
import com.zbu.friend.domain.question.vo.QuestionDetailVO;
import com.zbu.friend.domain.question.vo.QuestionVO;
import com.zbu.friend.elasticsearch.QuestionRepository;
import com.zbu.friend.manager.QuestionCacheManager;
import com.zbu.friend.mapper.question.QuestionMapper;
import com.zbu.friend.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    QuestionRepository questionRepository;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    QuestionCacheManager questionCacheManager;
    @Override
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO) {
        long count=questionRepository.count();
        if(count<=0){
            refreshQuestion();
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable= PageRequest.of(questionQueryDTO.getPageNum()-1,questionQueryDTO.getPageSize(), sort);
        Integer difficulty=questionQueryDTO.getDifficulty();
        String keyword=questionQueryDTO.getKeyword();
        Page<QuestionES>questionESPage;
        if(difficulty==null&& StrUtil.isEmpty(keyword)){
            questionESPage= questionRepository.findAll(pageable);
        }else if(StrUtil.isEmpty(keyword)){
            questionESPage=questionRepository.findQuestionByDifficulty(difficulty,pageable);
        }else if (difficulty==null){
            questionESPage=questionRepository.findByTitleOrContent(keyword,keyword,pageable);
        }else{
            questionESPage= questionRepository.findByTitleOrContentAndDifficulty(keyword,keyword,difficulty,pageable);
        }
        long totalElements = questionESPage.getTotalElements();
        if(totalElements==0){
            return TableDataInfo.empty();
        }
        List<QuestionES> content = questionESPage.getContent();
        List<QuestionVO>questionVOS=BeanUtil.copyToList(content,QuestionVO.class);
        return TableDataInfo.success(questionVOS,questionVOS.size());
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        QuestionES questionES = questionRepository.findById(questionId).orElse(null);
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        if(questionES!=null){
            BeanUtil.copyProperties(questionES,questionDetailVO);
            return questionDetailVO;
        }
        Question question = questionMapper.selectById(questionId);
        if(question==null){
            return null;
        }
        refreshQuestion();
        BeanUtil.copyProperties(questionDetailVO,question);
        return questionDetailVO;
    }


    private void refreshQuestion() {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>());
        if (CollectionUtil.isEmpty(questionList)) {
            return;
        }
        List<QuestionES> questionESList = BeanUtil.copyToList(questionList, QuestionES.class);
        questionRepository.saveAll(questionESList);
    }

    public String preQuesstion(Long quesstionId) {
        Long listSize= questionCacheManager.getListSize();
        if(listSize==null||listSize<=0){
            questionCacheManager.refreshCache();
        }
        return questionCacheManager.preQuestion(quesstionId).toString();
    }
    @Override
    public String nextQuesstion(Long quesstionId) {
        Long listSize= questionCacheManager.getListSize();
        if(listSize==null||listSize<=0){
            questionCacheManager.refreshCache();
        }
        return questionCacheManager.nextQuestion(quesstionId).toString();
    }

}
